#include <iostream>
#include <fstream>
#include "contacts.pb.h"

void PrintContacts(contacts::Contacts &con)
{
    for (int i = 0; i < con.people_size(); i++)
    {
        std::cout << "----------联系人" << i + 1 << "----------" << std::endl;
        const ::contacts::PeopleInfo &people = con.people(i);
        std::cout << "姓名：" << people.name() << std::endl;
        std::cout << "年龄：" << people.age() << std::endl;
        for (int j = 0; j < people.phone_size(); j++)
        {
            const ::contacts::PeopleInfo_Phone &phone = people.phone(j);
            std::cout << "号码" << j + 1 << ": " << phone.number();
            std::cout << "   (" << phone.PhoneType_Name(phone.type()) << ")" << std::endl;
        }

        if (people.has_data() && people.data().Is<contacts::Address>())
        {
            contacts::Address address;
            people.data().UnpackTo(&address);
            if (!address.home_add().empty())
            {
                std::cout << "联系人家庭地址：" << address.home_add() << std::endl;
            }
            if (!address.unit_add().empty())
            {
                std::cout << "联系人单位地址：" << address.unit_add() << std::endl;
            }
        }

        switch (people.other_contact_case())
        {
        case contacts::PeopleInfo::OtherContactCase::kQq:
            std::cout << "联系人qq：" << people.qq() << std::endl;
            break;
        case contacts::PeopleInfo::OtherContactCase::kWechat:
            std::cout << "联系人wechat：" << people.wechat() << std::endl;
            break;
        default:
            break;
        }

        if(people.remark_size())
        {
            std::cout<<"备注信息："<<std::endl;
            for(auto it = people.remark().cbegin(); it != people.remark().cend(); it++)
            {
                std::cout<<"   "<<it->first<<": "<<it->second<<std::endl;
            }
        }
    }
}

int main()
{
    contacts::Contacts con;
    // 读取已经存在的通讯录文件
    std::fstream input("con.bin", std::ios::in | std::ios::binary);
    if (!con.ParseFromIstream(&input))
    {
        std::cerr << "读取失败!" << std::endl;
        input.close();
        return -1;
    }

    // 答应通讯录列表
    PrintContacts(con);

    return 0;
}